package co.com.colcomercio.conf.dao.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import co.com.colcomercio.conf.dao.DAOISuper;
import co.com.colcomercio.util.ManejoParam;
import co.com.colcomercio.util.constants.GeneralConstants;
import co.com.colcomercio.util.exception.DAOException;

public class DAOSuper implements DAOISuper{
	
	public void truncarTabla(String tabla) throws DAOException
	{

		Connection con 			= null;
		PreparedStatement pre 	= null;				

		
			try
			{
				String sql = "truncate table "+tabla;										
												
				con = getConnection();
				
				pre = con.prepareStatement(sql );				
				
				pre.execute();
				
			}
			catch(Exception e)
			{
				Logger.getLogger(DAOSuper.class.getName()).log(Level.SEVERE, "Problema truncando tabla "+tabla, e);
				throw new DAOException("Problema truncando tabla "+tabla +GeneralConstants.ESPACE+e);
			}
			finally
			{				
				closePreparedStatement(pre);
				closeConnection(con);
			}
		
		
	}
	
    public Connection getConnection () throws SQLException {
    	
    	Connection conn = null;
    	
    	try
    	{
    		if(ManejoParam.getParam("PRUEBAS").equals("NO"))
        	{    			
    			Class.forName("oracle.jdbc.OracleDriver");
                conn = DriverManager.getConnection(ManejoParam.getParam("oracle.url"), ManejoParam.getParam("oracle.user"), ManejoParam.getParam("oracle.password"));    		
        	}
        	else
        	{
        		Class.forName("org.postgresql.Driver");        
                conn = DriverManager.getConnection(ManejoParam.getParam("postgresql.url"), ManejoParam.getParam("postgresql.user"), ManejoParam.getParam("postgresql.password"));    		    		
        	}    		
    	}
    	catch(Exception e)
    	{
    		throw new SQLException("Problema estableciendo conexion "+e);
    	}
                
        return conn;
    }
	

	protected void closeConnection(Connection conn)
    {
    	try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
    protected void closePreparedStatement(PreparedStatement pre)
    {
    	try {
			pre.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
    protected void closeCallableStatement(CallableStatement call)
    {
    	try {
			call.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
    protected void closeResultSet(ResultSet rs)
    {
    	try {
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
}
